<template>
	<section>
		<!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="advancedQuery">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" width="300px" label="商机名称" sortable>
			</el-table-column>
      <el-table-column prop="productName" width="200px" label="产品名称" sortable>
      </el-table-column>
      <el-table-column prop="productPrice" width="100px" label="产品价格" sortable>
      </el-table-column>
      <el-table-column prop="state" width="100px" label="状态" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: #5e7382">缴纳定金</span>
          <span v-if="scope.row.state == 2" style="color: #50bfff">成单</span>
          <span v-if="scope.row.state == -1" style="color: lawngreen">商机池</span>
          <span v-if="scope.row.state == 0" style="color: aquamarine">跟进中</span>
        </template>
      </el-table-column>
      <el-table-column prop="owner" width="100px" label="营销人员" sortable>
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">跟进</el-button>
					<el-button type="danger" size="small" @click="handleChange(scope.$index, scope.row)">变更</el-button>
					<el-button type="danger" v-if="scope.row.state == 0" size="small" @click="handleScrap(scope.$index, scope.row)">废弃</el-button>
					<el-button type="danger" v-if="scope.row.state == 0" size="small" @click="handleDeposit(scope.$index, scope.row)">交定金</el-button>
					<el-button type="danger" v-if="scope.row.state == 0 || scope.row.state == 1" size="small" @click="handleOrder(scope.$index, scope.row)">下单</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-col :span="24" class="toolbar">
      <!--分页条-->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="pageSizes"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>

    <el-dialog title="缴纳定金"
               :visible.sync="depositFormVisible"
               :close-on-click-modal="false">
      <el-form
          :model="depositForm"
          label-width="80px"
          ref="depositForm">
        <el-form-item label="产品名称">
          <el-input disabled v-model="depositForm.name"></el-input>
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input disabled v-model="depositForm.productName"></el-input>
        </el-form-item>
        <el-form-item label="产品价格">
          <el-input disabled v-model="depositForm.productPrice"></el-input>
        </el-form-item>
        <el-form-item label="定金">
          <el-input  v-model="depositForm.deposit"></el-input>
        </el-form-item>
        <el-form-item label="营销活动">
          <el-select v-model="depositForm.activityId" multiple clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in activities"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
        <el-button @click.native="depositFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveDeposit" :loading="depositLoading">确认</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
        pageInfo:{
          rows: [],
          total: 0,
        },
        pageSizes:[5, 10, 20, 50],
        //查询对象
        query:{
				  //当前页,默认第一页
          currentPage:1,
          //页面显示数据大小,默认显示5条
          pageSize:5,
          //高级查询关键字
          keyword:""
        },
        depositForm:{
          name:'',
          productName:'',
          productPrice:'',
          deposit:'',
          activityId:null,
        },
        para : {
          businessId : null,
          clueId:null,
          activityId: [],
          deposit:'',
        },
        depositFormVisible:false,
        depositLoading:false,
        activities:[],
      }
		},
		methods: {
		  //to交定金
      handleDeposit(index,row){
        this.depositFormVisible = true;
        this.depositForm.name = row.name;
        this.depositForm.productName = row.productName;
        this.depositForm.productPrice = row.productPrice;
        this.depositForm.deposit = row.deposit;
        this.getActivities();
        this.para.clueId = row.clueId;
        this.para.businessId = row.id;


      },
      //保存定金
      saveDeposit(){


        this.para.activityId = this.depositForm.activityId;
        this.para.deposit = this.depositForm.deposit;
        console.log("交定金信息")
        console.log(this.para)
        this.$http.post("business/saveDeposit",this.para)
            .then(result => {
              result = result.data;
              this.$message({
                message: result.message,
                type: 'success'
              });
            })
            .catch(result =>{
              this.$message({
                message: result.message,
                type: 'error'
              });
            })
        this.depositFormVisible = false;
        this.advancedQuery();
      },
      //获取活动列表
      getActivities() {
        this.$http.get("/activity")
            .then(result => {
              result = result.data;
              this.activities = result.obj;
            })
            .catch(result =>{
              this.$message({
                message: result.message,
                type: 'error'
              });
            })
      },
      //废弃
      handleScrap(){

      },
      //变更销售人员
      handleChange(){

      },
      //下单
      handleOrder(){

      },
			handleCurrentChange(val) {
			  //改变当前显示页数
        this.query.currentPage = val;
        this.getBusinesss();
			},
      handleSizeChange(val) {
			  //改变每页显示条数
        this.query.pageSize = val;
        this.getBusinesss();
      },
			//获取列表
			getBusinesss() {
				this.listLoading = false;
				this.$http.post("/business",this.query)
        .then(result => {
          result = result.data;
          console.log(result.obj.total);
          console.log(result.obj.rows);
          this.pageInfo = result.obj;
        })
        .catch(result =>{
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        })
			},
      advancedQuery(){
        //初始化页面
        this.query.currentPage = 1;
        //加载数据到页面
        this.getBusinesss();
      },
			//删除
			handleDel: function (index, row) {
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          //弹出加载窗口
          this.listLoading = true;
          this.$http.delete("/business/"+row.id)
              .then(result => {
                //操作成功进入then,关闭加载窗口
                this.listLoading = false;
                result = result.data;
                if(result.success){
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //删除完成后刷新页面
                  this.advancedQuery();
                }else{
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        //弹出界面
        this.saveFormVisible = true;
        this.saveForm = Object.assign({}, row);
        this.getTypeTree();
			},
      //获取所有类型
      getTypeTree(){
			  this.$http.get("/business/tree")
            .then(result =>{
              result = result.data;
              if (result.success){
                this.typeTree = result.obj;
              }
            })
            .catch(result => {

            })
      },
			//显示新增界面
			handleAdd: function () {
        //弹出界面
        this.saveFormVisible = true;
        //初始化
        this.saveForm = {
          id:null,
          name:'',
          price: '',
          description:'',
          resource:'',
          typeId:''
        }
        this.getTypeTree();

      },
			//保存
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.saveForm);

              if(para.typeId){
                para.typeId = para.typeId[para.typeId.length-1];
              }

              /*console.log("保存的数据")
              console.log(para)
              return ;*/
              this.$http.put("/business",para)
                  .then(result => {
                    result = result.data;
                    if(result.success){
                      this.advancedQuery();

                      this.saveFormVisible = false;
                      this.$message({ message: '保存成功',type: 'success'});
                    }else{
                      this.$message({ message: result.message,type: 'error'});
                    }
                    this.saveLoading = false;

                  })
            });
          }
        });
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
      batchDelete: function () {
        //从sels中获取所有被选中的id
        var ids = this.sels.map(item => item.id);
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //除get请求其他请求传参应该加上requestBody否侧无数据
          this.$http.patch("/business",ids)
              .then(result => {
                //无论成功与否都要关闭加载窗口
                this.listLoading = false;
                //后端返回的数据会被包在data中
                result = result.data;
                if(result.success){
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //刷新页面
                  this.advancedQuery();
                }else{
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
      },
		},
		mounted() {
			this.getBusinesss();
		}
	}

</script>

<style scoped>

</style>